package com.elitel.frame.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elitel.frame.base.component.RedisCacheComponent;
import com.elitel.frame.base.service.CfgExtrepService;
import com.elitel.frame.main.dao.CfgExtrepMapper;
import com.elitel.frame.main.dao.ext.ExtBaseRepconfigMapper;
import com.elitel.frame.main.dao.ext.ExtCfgDbMapper;
import com.elitel.frame.main.dao.ext.ExtCfgExtrepMapper;
import com.elitel.frame.main.entity.CfgDb;
import com.elitel.frame.main.entity.CfgExtrep;
import com.elitel.frame.main.entity.CfgTable;
import com.elitel.frame.main.entity.vo.ZTreeDataModelVo;

@Service
public class CfgExtrepServiceImpl implements CfgExtrepService{
	
	@Autowired
	private CfgExtrepMapper cfgExtrepMapper;
	@Autowired
	private ExtCfgDbMapper extCfgDbMapper;
	@Autowired
	private ExtCfgExtrepMapper extCfgExtrepMapper;
	@Autowired
	private ExtBaseRepconfigMapper extBaseRepconfigMapper;
	@Autowired
	private RedisCacheComponent redisCacheComponent;
	
	@Override
	public CfgExtrep selectCfgExtrepByPrimary(String guid) {
		CfgExtrep cfgExtrep = cfgExtrepMapper.selectByPrimaryKey(guid);
		return cfgExtrep;
	}

	@Override
	public List<ZTreeDataModelVo> getAllTreeList() {
		List<ZTreeDataModelVo> ztreeList = new ArrayList<ZTreeDataModelVo>();
		List<CfgDb> cfgdbList = extCfgDbMapper.getAllListCfgDb();
		if(cfgdbList != null && cfgdbList.isEmpty() == false)
		{
			for (CfgDb cb : cfgdbList) {
				ZTreeDataModelVo ztmodelDataModelVo = new ZTreeDataModelVo();
				ztmodelDataModelVo.setId(cb.getDbKey());
				ztmodelDataModelVo.setName(cb.getDbMark()+"("+cb.getDbName()+")");
				ztmodelDataModelVo.setHint("数据库版本:"+cb.getDbVersion()+",数据库名称:"+cb.getDbName());
				ztmodelDataModelVo.setIsMark("1");
				List<CfgExtrep> cfgExtrepList = extCfgExtrepMapper.selectByDtDbKey(cb.getDbKey());
				if(cfgExtrepList !=null && cfgExtrepList.isEmpty() == false)
				{
					List<ZTreeDataModelVo> ztreeTable = new ArrayList<ZTreeDataModelVo>();
					for (CfgExtrep cfgt : cfgExtrepList) {
						ZTreeDataModelVo zttable = new ZTreeDataModelVo();
						zttable.setId(cfgt.getGuid());
						zttable.setName(cfgt.getServicename());
						zttable.setHint("数据库:"+cb.getDbName()+",报表:"+cfgt.getServicename());
						zttable.setIsMark("2");
						ztreeTable.add(zttable);
					}
					ztmodelDataModelVo.setChildren(ztreeTable);
				}
				
				ztreeList.add(ztmodelDataModelVo);
			}
		}
		
		
		return ztreeList;
	}

	@Override
	public void deleteExtrepByGuid(String guid) {
		cfgExtrepMapper.deleteByPrimaryKey(guid);
		extBaseRepconfigMapper.deleteRepConfigByRguid(guid);
		redisCacheComponent.freshTreeReportData();
	}

	@Override
	public Integer insertExtrep(CfgExtrep cfgExtrep) {
		int insertSelective = cfgExtrepMapper.insertSelective(cfgExtrep);
		redisCacheComponent.freshTreeReportData();
		return insertSelective;
	}

	@Override
	public Integer updateExtrepByGuid(CfgExtrep cfgExtrep) {
		int updateByPrimaryKeySelective = cfgExtrepMapper.updateByPrimaryKeySelective(cfgExtrep);
		redisCacheComponent.freshTreeReportData();
		return updateByPrimaryKeySelective;
	}

}
